/*
 * StartApplication.java
 *
 * Created on October 8, 2006, 12:15 AM
 */

package org.sunspotworld;

import com.sun.spot.core.peripheral.Spot;
import com.sun.spot.core.resources.transducers.ITriColorLED;
import com.sun.spot.core.resources.transducers.LEDColor;
import com.sun.spot.core.util.Utils;
import com.sun.spot.edemo.EDemoBoard;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

/**
 * The startApp method of this class is called by the VM to start the
 * application.
 *
 * The manifest specifies this class as MIDlet-1, which means it will
 * be selected for execution.
 */
public class StartApplication extends MIDlet {
    private static final String RECORD_KEY = "DEMO";
    private static final String PROPERTY_KEY = "Testing";
    private RecordStore rms;
    private ITriColorLED leds[] = EDemoBoard.getInstance().getLEDs();
    private static final int WRITE_THIS_NUMBER = 5;
    private int index = 0;
    private byte[] buffer;

    private void initialize() {
        try {
            rms = RecordStore.openRecordStore(RECORD_KEY, true);
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
    }

    private void write() {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        try {
            for (int i = 0; i < 20; i++) {
                dos.writeInt(WRITE_THIS_NUMBER);
                dos.flush();
                buffer = baos.toByteArray();
                rms.addRecord(buffer, 0, buffer.length);
                displayNumber(index + 1, LEDColor.ORANGE);
                index++;
                Utils.sleep(500);
            }

            displayNumber(5, LEDColor.CYAN);
            Utils.sleep(500);
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        } catch (IOException iOException) {
        }
    }
    
    private void read() {
        DataInputStream dis;
        try {
            index = 0;
            RecordEnumeration ren = rms.enumerateRecords(null, null, false);
            while (ren.hasNextElement()) {
                try {
                    dis = new DataInputStream(new ByteArrayInputStream(ren.nextRecord()));
                    if (WRITE_THIS_NUMBER == dis.readInt()) {
                        displayNumber(index + 1, LEDColor.ORANGE);
                        index++;
                        Utils.sleep(500);
                    }
                } catch (IOException iOException) {
                }
            }
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
    }

    private void displayNumber(int val, LEDColor col) {
        for (int i = 0, mask = 1; i < 7; i++, mask <<= 1) {
            leds[7-i].setColor(col);
            leds[7-i].setOn((val & mask) != 0);
        }
    }

    protected void startApp() throws MIDletStateChangeException {
        Spot.getInstance().setPersistentProperty(PROPERTY_KEY, "You Bet!");
        System.out.println(Spot.getInstance().getPersistentProperty(PROPERTY_KEY));
        initialize();
        write();
        read();
        notifyDestroyed();
    }

    protected void pauseApp() {
        // This will never be called by the Squawk VM
    }

    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
        // Only called if startApp throws any exception other than MIDletStateChangeException
    }
}
